﻿Public Class frmTacGia

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        PhucHoiNutSua()
        clsTacGia.Them(txtMaTG.Text, txtTenTG.Text, txtDVCT.Text, txtDiaChi.Text, txtDienThoai.Text)
        datTacGia.DataSource = clsTacGia.DSTacGia
        btnLamLai_Click(sender, e)

    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        PhucHoiNutSua()

        If MsgBox("Bạn có muốn xóa tác giả có mã là " & txtMaTG.Text.Trim & " không ?", MsgBoxStyle.YesNo, "Thông báo") = MsgBoxResult.Yes Then

            If Not clsTacGia.Xoa(txtMaTG.Text.Trim) Then
                MsgBox("Không thể xóa tác giả có mã " & txtMaTG.Text & ".Có thể sách của tác giả này vẫn còn tồn tại!", MsgBoxStyle.Critical, "Loi")
            End If

            datTacGia.DataSource = clsTacGia.DSTacGia

        End If

    End Sub

    Private Sub datTacGia_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles datTacGia.CellDoubleClick
        PhucHoiNutSua()
        btnSua_Click(sender, e)

    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If btnSua.Text = "Sửa" Then
            txtMaTG.Text = datTacGia.CurrentRow.Cells("MaTG").Value
            txtTenTG.Text = datTacGia.CurrentRow.Cells("TenTG").Value
            txtDVCT.Text = datTacGia.CurrentRow.Cells("DonViCongTac").Value
            txtDiaChi.Text = datTacGia.CurrentRow.Cells("DiaChi").Value
            txtDienThoai.Text = datTacGia.CurrentRow.Cells("DienThoai").Value
            btnSua.Text = "Lưu lại"
        Else
            btnSua.Text = "Sửa"
            clsTacGia.Sua(txtMaTG.Text, txtTenTG.Text, txtDVCT.Text, txtDiaChi.Text, txtDienThoai.Text)
            datTacGia.DataSource = clsTacGia.DSTacGia
            btnLamLai_Click(sender, e)
        End If


    End Sub

    Private Sub frmTacGia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        datTacGia.DataSource = clsTacGia.DSTacGia

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Hide()

    End Sub
    Private Sub PhucHoiNutSua()
        If btnSua.Text = "Lưu lại" Then
            btnSua.Text = "Sửa"
        End If
    End Sub

    Private Sub btnLamLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLamLai.Click
        PhucHoiNutSua()
        txtMaTG.Text = ""
        txtTenTG.Text = ""
        txtDiaChi.Text = ""
        txtDienThoai.Text = ""
        txtDVCT.Text = ""
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        PhucHoiNutSua()
        datTacGia.DataSource=clsTacGia.TimKiem(txtTuKhoa.Text)
    End Sub

    Private Sub btnReLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReLoad.Click
        datTacGia.DataSource = clsTacGia.DSTacGia
        txtTuKhoa.Text = ""

    End Sub

End Class